package com.anli.service;

import com.alibaba.fastjson.JSON;
import com.anli.dao.TestLockMapper;
import com.anli.entity.StockInfo;
import org.redisson.Redisson;
import org.redisson.RedissonRedLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/30  12:05
 * 描述:
 */
@Component
public class TestLockService {

    @Autowired
    private TestLockMapper testLockMapper;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired(required = false)
    //@Qualifier("redissonClient1")
    private RedissonClient redissonClient1;
    @Autowired(required = false)
    //@Qualifier("redissonClient2")
    private RedissonClient redissonClient2;
    @Autowired(required = false)
    //@Qualifier("redissonClient3")
    private RedissonClient redissonClient3;


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public void testLock(StockInfo stockInfo){

        RLock lock = redissonClient.getLock(stockInfo.getGoodsId() + "-" + stockInfo.getStockId());

        lock.lock();
        try{
            StockInfo stockInfo1 = testLockMapper.getStockInfo(stockInfo);
                if((stockInfo1.getStock()-stockInfo.getStock())<0){
                    System.out.println("======库存不足======");
                    return ;
                }else{
                stockInfo.setStock(stockInfo1.getStock()-stockInfo.getStock());
                testLockMapper.updateStock(stockInfo);
                System.out.println("======库存======"+(stockInfo1.getStock()-stockInfo.getStock()));
            }
        }catch (Exception e){
            System.out.println("======处理出错======");
        }finally {
            lock.unlock();
        }

    }


    public void testLock1(StockInfo stockInfo){

        try{
            StockInfo stockInfo1 = testLockMapper.getStockInfo(stockInfo);
            if((stockInfo1.getStock()-stockInfo.getStock())<0){
                System.out.println("======库存不足======");
                return ;
            }else{
                stockInfo.setStock(stockInfo1.getStock()-stockInfo.getStock());
                testLockMapper.updateStock(stockInfo);
            }

        }catch (Exception e){
            System.out.println("======处理出错======");
        }

    }



    //redLock的使用
    public void yyyy(StockInfo stockInfo) throws InterruptedException {

        RLock key1 = redissonClient1.getLock("key");
        RLock key2 = redissonClient2.getLock("key");
        RLock key3 = redissonClient3.getLock("key");

        RedissonRedLock redissonRedLock=new RedissonRedLock(key1,key2,key3);

        redissonRedLock.tryLock(1000,TimeUnit.SECONDS);

        try{
            StockInfo stockInfo1 = testLockMapper.getStockInfo(stockInfo);
            if((stockInfo1.getStock()-stockInfo.getStock())<0){
                System.out.println("======库存不足======");
                return ;
            }else{
                stockInfo.setStock(stockInfo1.getStock()-stockInfo.getStock());
                testLockMapper.updateStock(stockInfo);
            }

        }catch (Exception e){
            System.out.println("======处理出错======");
        }finally {
            redissonRedLock.unlock();
        }


    }






    /**
     * 作者: LI-CI-GAO
     * 日期: 2021/8/30  15:52
     * 描述: 乐观锁的实现
     * @Param []
     * @Return void
     */
     @Autowired
     @Qualifier("redisTemplate10")
     private RedisTemplate<String,String> redisTemplate10;


     public void leGUanSuo(StockInfo stockInfo){
         //1、获取库存
         StockInfo stockInfo1 = testLockMapper.getStockInfo(stockInfo);
         //2、检验库存
         if((stockInfo1.getStock()-stockInfo.getStock())<0){
            //说明库存不足
         }else{
            //3、更新锁定库存
             StockInfo stockInfo2=new StockInfo();
             stockInfo2.setStockId(stockInfo.getStockId());
             stockInfo2.setGoodsId(stockInfo.getGoodsId());
             stockInfo2.setStock(stockInfo1.getStock()-stockInfo.getStock());
             stockInfo2.setVersion(stockInfo1.getVersion());
             int i = testLockMapper.updateStock2(stockInfo2);
             if(i==0){
                //4、更新失败
                 String string = JSON.toJSONString(stockInfo);
                 System.out.println("=======加入队列1======"+string);
                 //redisTemplate10.convertAndSend("LOCK",string);
             }
         }




     }


}
